<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Comment extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'comment';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'to_reply_type_text',
        'reply_type_text'
    ];
    


    
    public function getToReplyTypeList()
    {
        return ['0' => __('To_reply_type 0'), '1' => __('To_reply_type 1'), '2' => __('To_reply_type 2')];
    }

    public function getReplyTypeList()
    {
        return ['0' => __('Reply_type 0'), '1' => __('Reply_type 1'), '2' => __('Reply_type 2')];
    }


    public function getToReplyTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['to_reply_type']) ? $data['to_reply_type'] : '');
        $list = $this->getToReplyTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getReplyTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['reply_type']) ? $data['reply_type'] : '');
        $list = $this->getReplyTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    //获取当前校区的线索标签 主要用于添加时候
    public static function insertComment($shopId, $param){
        if($param['to_id']){
            $where['id'] = $param['to_id'];
            $comment = db('comment')->where($where)->field('id,reply_type,top_id,reply_id')->find();
            $addParam['to_reply_type'] = $comment['reply_type'];
            $addParam['to_reply_id'] = $comment['reply_id'];
            $addParam['top_id'] = $comment['top_id'];
            if(empty($comment['top_id'])){
                $addParam['top_id'] = $comment['id'];
            }
        }
        $addParam['to_id'] = $param['to_id'];
        $addParam['shop_id'] = $shopId;
        $addParam['style'] = $param['style'];
        $addParam['relation_id'] = $param['relation_id'];
        $addParam['reply_type'] = $param['reply_type'];
        $addParam['reply_id'] = $param['reply_id'];
        $addParam['content'] = $param['content'];
        $addParam['createtime'] = time();
        $labelId = db('comment')->insertGetId($addParam);
        switch($param['style']){
            case 'Article': //文章
                db("article")->where("id", $param['relation_id'])->setInc("comments", 1);
                break;
            case 'Interact': //互动讨论
                db("interact")->where("id", $param['relation_id'])->setInc("comments", 1);
                break;
            case 2: //用户

                break;
        }

        return $labelId;
    }

    //获取当前校区的线索标签 主要用于添加时候
//    public static function getComments($relation_id){
//        $where['relation_id'] = $relation_id;
//        $comment = model('comment')->where($where)->select()->toArray();
//        foreach($comment as &$val){
//            $val['reply_name'] = self::getUser($val['reply_id'],$val['reply_type']);
//            if($val['to_id']){
//                $val['to_reply_name'] = self::getUser($val['to_reply_id'],$val['to_reply_type']);
//            }
//        }
//        return $comment;
//    }
    //获取当前校评论列表
    public static function getComments($relation_id,$to_id,$style){
        if($to_id){
            $where['status'] = 0;
        }
        $where['to_id'] = 0;
        $where['relation_id'] = $relation_id;
        $where['style'] = $style;
        $comment = model('comment')->where($where)->select()->toArray();
        foreach($comment as &$val){
            $reply = self::getUser($val['reply_id'],$val['reply_type']);
            $val['reply_name'] = $reply['username'];
            $val['reply_img'] = $reply['avatar'];
            if($val['to_id']){
                $to_reply = self::getUser($val['to_reply_id'],$val['to_reply_type']);
                $val['to_reply_name'] = $to_reply['username'];
                $val['to_reply_img'] = $to_reply['avatar'];
            }
            $val['createtime_txt'] = date('Y-m-d',$val['createtime']);

            //获取评论
            $val['replyList'] = self::replyList($val['id']);
        }
        return $comment;
    }

    //回复评论列表
    public static function replyList($id){
        $where['top_id'] = $id;
        $comment = model('comment')->where($where)->select()->toArray();
        foreach($comment as &$val){
            $reply = self::getUser($val['reply_id'],$val['reply_type']);
            $val['reply_name'] = $reply['username'];
            $val['reply_img'] = $reply['avatar'];
            if($val['to_id']){
                $to_reply = self::getUser($val['to_reply_id'],$val['to_reply_type']);
                $val['to_reply_name'] = $to_reply['username'];
                $val['to_reply_img'] = $to_reply['avatar'];
            }
            $val['createtime_txt'] = date('Y-m-d',$val['createtime']);
        }
        return $comment;
    }

    //获取评论人
    private static function getUser($id,$type){
        $where['id'] = $id;
        switch($type){
            case 0: //总部
                $username = model('admin')->where($where)->value('username');
                break;
            case 1: //校区服务商
                $username = model('manystore')->where($where)->value('username');
                break;
            case 2: //用户
                $username = model('user')->where($where)->value('truename');
        }
        return $username;
    }



}
